
package joodin.impl.widgets.internal;

import java.util.Map;

import joodin.impl.widgets.VaadinContainer;
import my.stuff.vaadin.miglayout.MigLayout;
import my.stuff.vaadin.miglayout.util.ComponentSizeUtil;
import my.stuff.vaadin.miglayout.util.Size;
import my.stuff.vaadin.miglayout.util.SizeConverter;

import org.jowidgets.common.color.IColorConstant;
import org.jowidgets.common.types.Cursor;
import org.jowidgets.common.types.Dimension;
import org.jowidgets.common.widgets.IControlCommon;
import org.jowidgets.common.widgets.controler.IPopupDetectionListener;
import org.jowidgets.common.widgets.descriptor.IWidgetDescriptor;
import org.jowidgets.common.widgets.factory.ICustomWidgetFactory;
import org.jowidgets.common.widgets.factory.IGenericWidgetFactory;
import org.jowidgets.common.widgets.layout.ILayoutDescriptor;
import org.jowidgets.spi.widgets.IPopupMenuSpi;
import org.jowidgets.spi.widgets.IScrollCompositeSpi;
import org.jowidgets.spi.widgets.setup.IScrollCompositeSetupSpi;

import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Panel;
import com.vaadin.ui.themes.Runo;

public class ScrollCompositeImpl implements IScrollCompositeSpi {

	private VaadinContainer innerContainer;
	private VaadinContainer outerContainer;

	@SuppressWarnings("serial")
	public ScrollCompositeImpl(final IGenericWidgetFactory factory, final IScrollCompositeSetupSpi setup) {
		
		outerContainer = new VaadinContainer(factory, new Panel());
		
		getUiReference().addStyleName(Runo.PANEL_LIGHT);
			
		innerContainer = new VaadinContainer(factory, new Panel(){
			@Override
			public String toString() {
				return "ScrollComposite@" + hashCode();
			}
		});

		if(setup.getBorder() == null){
			innerContainer.getUiReference().addStyleName(Runo.PANEL_LIGHT);
		}
		else{
			innerContainer.getUiReference().setCaption(setup.getBorder().getTitle());
		}

//		final int horizontalPolicy = ScrollBarSettingsConvert.convertHorizontal(setup);
//		final int verticalPolicy = ScrollBarSettingsConvert.convertVertical(setup);
//
//		
//		scrollPane.setVerticalScrollBarPolicy(verticalPolicy);
//		scrollPane.setHorizontalScrollBarPolicy(horizontalPolicy);

		innerContainer.setLayout(setup.getLayout());
		innerContainer.getUiReference().setSizeFull();
		
		outerContainer.getUiReference().setContent(new MigLayout("", "[grow, fill]", "[grow, fill]"){
			@Override
			public void changeVariables(Object source, Map variables) {
				Size size = ComponentSizeUtil.getPreferredSize(innerContainer.getUiReference());
				int width = SizeConverter.getPixelWidth(outerContainer.getUiReference());
				int height = SizeConverter.getPixelHeight(outerContainer.getUiReference());
				
				if( width < size.getWidth()){
					((MigLayout)innerContainer.getUiReference().getContent()).setWidth(size.getWidth(), Sizeable.UNITS_PIXELS);
				}
				else{
					((MigLayout)innerContainer.getUiReference().getContent()).setWidth("100%");
				}
				
				if( height < size.getHeight()){
					((MigLayout)innerContainer.getUiReference().getContent()).setHeight(size.getHeight(), Sizeable.UNITS_PIXELS);
				}
				else{
					((MigLayout)innerContainer.getUiReference().getContent()).setHeight("100%");
				}
				
				super.changeVariables(source, variables);
			}
		});
		innerContainer.getUiReference().setSizeFull();
		((MigLayout)outerContainer.getUiReference().getContent()).addComponent("grow", innerContainer.getUiReference());
		
	}

	@Override
	public final void setLayout(final ILayoutDescriptor layout) {
		innerContainer.setLayout(layout);
	}

	@Override
	public Panel getUiReference() {
		return (Panel) outerContainer.getUiReference();
	}

	@Override
	public void redraw() {
		innerContainer.redraw();
	}

	@Override
	public void setVisible(final boolean visible) {
		innerContainer.setVisible(visible);
	}

	@Override
	public boolean isVisible() {
		return innerContainer.isVisible();
	}

	@Override
	public Dimension getSize() {
		return innerContainer.getSize();
	}

	@Override
	public void setEnabled(final boolean enabled) {
		innerContainer.setEnabled(enabled);
	}

	@Override
	public boolean isEnabled() {
		return innerContainer.isEnabled();
	}

	@Override
	public void setForegroundColor(final IColorConstant colorValue) {
		innerContainer.setForegroundColor(colorValue);
		innerContainer.setForegroundColor(colorValue);
	}

	@Override
	public void setBackgroundColor(final IColorConstant colorValue) {
		innerContainer.setBackgroundColor(colorValue);
		innerContainer.setBackgroundColor(colorValue);
	}

	@Override
	public IColorConstant getForegroundColor() {
		return innerContainer.getForegroundColor();
	}

	@Override
	public IColorConstant getBackgroundColor() {
		return innerContainer.getBackgroundColor();
	}

	@Override
	public void setCursor(final Cursor cursor) {
		innerContainer.setCursor(cursor);
	}

	@Override
	public final <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final IWidgetDescriptor<? extends WIDGET_TYPE> descriptor,
		final Object cellConstraints) {
		
		WIDGET_TYPE common = innerContainer.add(descriptor, cellConstraints);

		return common;
	}

	@Override
	public final <WIDGET_TYPE extends IControlCommon> WIDGET_TYPE add(
		final ICustomWidgetFactory<WIDGET_TYPE> factory,
		final Object cellConstraints) {
		
		WIDGET_TYPE common = innerContainer.add(factory, cellConstraints);
			
		Size size = ComponentSizeUtil.getPreferredSize(innerContainer.getUiReference());
		
		((MigLayout)innerContainer.getUiReference().getContent()).setWidth( size.getWidth() , Sizeable.UNITS_PIXELS);
		((MigLayout)innerContainer.getUiReference().getContent()).setHeight( size.getHeight(), Sizeable.UNITS_PIXELS);
		
		return common;
	}

	@Override
	public boolean remove(final IControlCommon control) {
		return innerContainer.remove(control);
	}

	@Override
	public void layoutBegin() {
		innerContainer.layoutBegin();
	}

	@Override
	public void layoutEnd() {
		innerContainer.layoutEnd();
	}

	@Override
	public void removeAll() {
		innerContainer.removeAll();
	}

	@Override
	public IPopupMenuSpi createPopupMenu() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addPopupDetectionListener(IPopupDetectionListener arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removePopupDetectionListener(IPopupDetectionListener arg0) {
		// TODO Auto-generated method stub
	}

	@Override
	public Object getLayoutConstraints() {
		return ((MigLayout)getUiReference().getContent()).getLayoutConstraints();
	}

	@Override
	public void setLayoutConstraints(Object arg0) {
		((MigLayout)getUiReference().getContent()).setLayoutConstraints(arg0);
	}

}
